package com.sumainfor.zuul.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.R;
import com.sumainfor.common.utlis.Jsapi_ticketUtil;
import com.sumainfor.common.wechat.WxConstant;
import com.sumainfor.common.wechat.WxEncodeUtil;
import com.sumainfor.zuul.service.SysUserTokenService;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sumainfor.zuul.service.impl.WxSysUserService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 功能描述:
 * 微信签名
 *
 * @Auther: zhlu
 * @Date: 2019/1/24 20:55
 * @Description:
 */
@RestController
@RequestMapping("/sys/wechat")
public class WechatController extends AbstractController {

    Logger log = LoggerFactory.getLogger(WechatController.class);

    @Autowired
    WxSysUserService sysUserService;


    @GetMapping("/signature")
    public JsonResult signature() {
        JsonResult result = new JsonResult();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //调用获取 access_token
            Jsapi_ticketUtil t = new Jsapi_ticketUtil();
            String access_token = t.getAccessToken(WxConstant.WECHATPAY_APPID, WxConstant.WECHATPAY_PAY_APPSECRET);
            log.info("access_token===============>" + access_token);
            //其次就是 通过access_token获取jsapi_ticket
            String jsapiTicket = t.getJsapiTicket(access_token);
            log.info("jsapiTicket===============>" + jsapiTicket);
            // 随机获取UUID
            String uuid = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
            log.info("uuid===============>" + uuid);
            //获取时间戳
            long time = System.currentTimeMillis() / 1000;
            String timestamp = String.format("%010d", time);
            log.info("time===============>" + time);

            String signature = t.getSign(uuid, jsapiTicket, timestamp, WxConstant.WECHATPAY_SIGN_URL);
            log.info("signature===============>" + signature);
            map.put("appId", WxConstant.WECHATPAY_APPID);
            map.put("timestamp", time);
            map.put("nonceStr", uuid);
            map.put("signature", signature);
            result.put(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.putFailed("获取签名失败");
        }
        return result;
    }

    /**
     * 功能描述:
     * 生成绑定二维码
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/1/25 16:42
     */
    @GetMapping("/getQrConnectURL")
    public JsonResult getQrConnectURL(@RequestParam Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"url"})) {
            result.putFailed("url不能为空！");
        }
        Map<String, String> weparams = new HashMap<String, String>();
        weparams.put("appid", WxConstant.WECHATPAY_APPID);
        weparams.put("redirect_uri", WxConstant.WECHATPAY_REDIRECT_URI + params.get("url"));
        weparams.put("response_type", "code");
        weparams.put("scope", "snsapi_login");
        String wechaturl = WxEncodeUtil.getQrConnectURL(weparams);
        result.put(new HashMap<String, Object>() {{
            put("wechaturl", wechaturl);
        }});
        return result;
    }

    /**
     * 功能描述:
     * 根据用户编号和code获取用户资料并且保存下来
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/1/26 10:09
     */
    @PostMapping("/BoundSysUserWechat")
    public JsonResult BoundSysUserWechat(@RequestBody Map<String, Object> params) throws ClientProtocolException, IOException {
        return sysUserService.BoundSysUserWechat(params);
    }

}
